<template>
  <span>
    <Button v-if="isClick" @click="onHandler" :type="type" :size="size" :icon="icon" :disabled="disabled"><slot></slot></Button>
    <Button v-else-if="isButton" :type="type" :size="size" :icon="icon" :disabled="disabled"><slot></slot></Button>
    <span else></span>
  </span>
</template>

<script>
/**
 * 权限Button，根据权限来判断权限是否显示
 */
export default {
  data () {
    return {
      show: false
    }
  },
  props: {
    type:{
      type:String,
      default:'info'
    },
    size: {
      type: String,
      default: 'default'
    },
    icon: String,
    perm: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default () {
        return false
      }
    },
    click: {
      type: Boolean,
      default () {
        return false
      }
    }
  },
  methods: {
    onHandler () {
      this.$emit('click');
    }
  },
  computed: {
    isClick () {
      return this.show && this.click
    },
    isButton () {
      return this.show && !this.click
    }
  },
  created() {
    if(this.perm === '') {
      //如果不传则直接有权限
      this.show = true;
    } else {
      //1.获取权限
      let permission = this.$store.state.app.currentPermission
      if(permission.length === 0) {
        permission = JSON.parse(localStorage.permission)
      }
      //判断权限
      for (let value of permission) {
        if(value === this.perm) {
          this.show = true;
          break;
        }
      }
    }
  }
}
</script>

<style>

</style>
